package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.modular.system.entity.BtyFh;
import cn.stylefeng.guns.sys.modular.system.entity.BtyWxf;
import cn.stylefeng.guns.sys.modular.system.entity.BtyYj;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyFhMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyWxfMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyWxfParam;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyYjParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyWxfResult;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyYjResult;
import  cn.stylefeng.guns.sys.modular.system.service.BtyWxfService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuy
 * @since 2021-11-24
 */
@Service
public class BtyWxfServiceImpl extends ServiceImpl<BtyWxfMapper, BtyWxf> implements BtyWxfService {
    @Resource
    private BtyFhMapper fhMapper;
    @Override
    public void add(BtyWxfParam param){
        BtyWxf entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BtyWxfParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BtyWxfParam param){
        BtyWxf oldEntity = getOldEntity(param);
        BtyWxf newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BtyWxfResult findBySpec(BtyWxfParam param){
        return null;
    }

    @Override
    public List<BtyWxfResult> findListBySpec(BtyWxfParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BtyWxfParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }
    /**
     * 导入excel
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean batchImport(String fileName, MultipartFile file, BtyWxfParam param){
        boolean notNull = false;
        try {
            if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
                throw new Exception("上传文件格式不正确");
            }
            boolean isExcel2003 = true;
            if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
                isExcel2003 = false;
            }
            InputStream is = file.getInputStream();
            Workbook wb = null;
            if (isExcel2003) {
                wb = new HSSFWorkbook(is);
            } else {
                wb = new XSSFWorkbook(is);
            }
            //excel中多个sheet表，获取所有sheet表的数量
            //int sheets = wb.getNumberOfSheets();
            //System.out.println("sheets:"+sheets);
            //导入之前删掉之前的数据
            baseMapper.delete(new QueryWrapper<>());
            String yf=param.getZdrq();
            //第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            if (sheet != null) {
                notNull = true;
            }

            int lastRowNum = sheet.getLastRowNum();
            for (int r = 1; r <= lastRowNum; r++) {
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }
                //获取单元格
                Cell cell = row.getCell(1);
                if (ToolUtil.isNotEmpty(cell)) {
                    cell.setCellType(CellType.STRING);
                }
                //店名
                String dm=row.getCell(0) == null ? "" : row.getCell(0).getStringCellValue();
                //维修费
                String wxf="";
                if(ToolUtil.isEmpty(row.getCell(1).toString().trim())){
                    wxf="0";
                }else {
                    wxf=row.getCell(1).getStringCellValue();
                }
                BtyWxf yj=new BtyWxf();
                yj.setDm(dm);
                yj.setWxf(new BigDecimal(wxf));
                baseMapper.insert(yj);
            }
            //excel导入成功之后获取押金总和明细保存到分红表中
            List<BtyWxf>list=baseMapper.selectList(new QueryWrapper<BtyWxf>().gt("wxf",0));
            for (BtyWxf result : list) {
                String dm=result.getDm();
                BigDecimal ye=result.getWxf();
                //根据店名查询对应的押金明细，逗号分隔拼接到一起
                BtyYjParam yjParam=new BtyYjParam();
                yjParam.setDm(dm);
                BtyFh fh=fhMapper.selectOne(new QueryWrapper<BtyFh>().eq("yf",yf).eq("dm",dm));
                if(ToolUtil.isNotEmpty(fh)){
                    fh.setWxf(ye);
                    fhMapper.updateById(fh);
                }
            }
        } catch (Exception e) {
            notNull=false;
            e.printStackTrace();
        }
        return notNull;
    }

    private Serializable getKey(BtyWxfParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BtyWxf getOldEntity(BtyWxfParam param) {
        return this.getById(getKey(param));
    }

    private BtyWxf getEntity(BtyWxfParam param) {
        BtyWxf entity = new BtyWxf();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
